package com.hhkj.study.admin.goods.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.hhkj.study.admin.goods.entity.Goods;
import com.hhkj.study.admin.goods.entity.GoodsSku;
import com.hhkj.study.admin.goods.model.podo.GoodsDo;
import com.hhkj.study.admin.goods.model.podo.GoodsPo;
import com.hhkj.study.admin.goods.model.podo.GoodsSkuDo;
import com.hhkj.study.admin.goods.model.podo.GoodsSkuPo;
import com.hhkj.study.admin.goods.model.req.GoodsReq;
import com.hhkj.study.admin.goods.model.res.GoodsRes;
import com.hhkj.study.admin.goods.service.base.GoodsService;
import com.hhkj.study.admin.goods.service.base.GoodsSkuService;
import com.hhkj.study.admin.goods.service.biz.GoodsBizService;
import com.hhkj.study.admin.goods.service.extend.GoodsExtendService;
import com.hhkj.study.admin.teacher.entity.Teacher;
import com.hhkj.study.admin.teacher.model.res.TeacherRes;
import com.hhkj.study.admin.teacher.service.base.TeacherService;
import com.hhkj.study.common.cache.GlobalCache;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.enums.DictCodeEnum;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* GoodsBizServiceImpl
*
* @author SERVER
* @since 2024-09-18
*/
@Service
public class GoodsBizServiceImpl extends BaseBizServiceImpl<Goods, GoodsReq, GoodsRes, GoodsDo, GoodsPo, GoodsService, GoodsExtendService> implements GoodsBizService {

    @Resource
    private GoodsSkuService goodsSkuService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private GlobalCache globalCache;

    @Override
    protected void afterList(List<GoodsRes> goodsResList, GoodsReq req) {
        Integer showTeacherFlag = req.getShowTeacherFlag();
        boolean showTeacher = showTeacherFlag == null ? true : showTeacherFlag == 1;
        if (showTeacher) {
            assignGoodsTeacher(goodsResList);
        }

        Integer showSkuFlag = req.getShowSkuFlag();
        boolean showSku = showSkuFlag == null ? false : showSkuFlag == 1;
        if (showSku) {
            assignGoodsSku(goodsResList);
        }
        assignOtherInfo(goodsResList);
    }


    @Override
    protected void afterSave(GoodsReq goodsReq) {
        super.afterSave(goodsReq);

        List<GoodsSkuPo> skuList = goodsReq.getSkuList();
        if (skuList == null || skuList.size() == 0){
            return;
        }

        saveBatchSku(goodsReq);
    }

    @Override
    protected void afterUpdate(GoodsReq goodsReq) {
        super.afterUpdate(goodsReq);

        List<GoodsSkuPo> skuList = goodsReq.getSkuList();
        if (skuList == null || skuList.size() == 0){
            return;
        }

        goodsSkuService.removeByGoodsId(goodsReq.getId());
        saveBatchSku(goodsReq);
    }

    @Override
    protected void afterPage(List<GoodsRes> goodsRes) {
        assignOtherInfo(goodsRes);
    }

    @Override
    protected void afterGetById(GoodsRes goodsRes) {
        super.afterGetById(goodsRes);

        GoodsSkuDo goodsSkuDo = new GoodsSkuDo();
        goodsSkuDo.setGoodsId(goodsRes.getId());
        List<GoodsSkuPo> skuList = goodsSkuService.listPoByDo(goodsSkuDo);
        goodsRes.setSkuList(skuList);
        assignGoodsTeacher(Lists.newArrayList(goodsRes));

        assignOtherInfo(Lists.newArrayList(goodsRes));
    }

    /**
     * 组装商品中的讲师列表
     * @param goodsResList
     */
    private void assignGoodsTeacher(List<GoodsRes> goodsResList) {
        if (CollectionUtil.isEmpty(goodsResList)) {
            return;
        }
        // 集合中所有老师id 通过流转化所有对象的idList放在一起后续逐个获取到对象
        Set<Long> teacherIdSet = goodsResList.stream().filter(e -> CollectionUtil.isNotEmpty(e.getTeacherIdList()))
                .flatMap(goodsRes -> goodsRes.getTeacherIdList().stream()).collect(Collectors.toSet());
        if (CollectionUtil.isEmpty(teacherIdSet)) {
            return;
        }
        LambdaQueryWrapper<Teacher> teacherQuery = new LambdaQueryWrapper<>();
        teacherQuery.in(Teacher::getId, teacherIdSet);
        List<Teacher> teacherList = teacherService.list(teacherQuery);
        goodsResList.forEach(goodsRes -> {
            List<Long> teacherIdList = goodsRes.getTeacherIdList();
            if (CollectionUtil.isEmpty(teacherIdList)) {
                return;
            }
            // tidList转化成ResList
            List<TeacherRes> tidToTeacherResList = teacherList.stream()
                    .filter(teacher -> teacherIdList.contains(teacher.getId()))
                    .map(teacher -> BeanUtil.toBean(teacher, TeacherRes.class))
                    .collect(Collectors.toList());
            goodsRes.setTeacherList(tidToTeacherResList);
        });
    }

    /**
     * 组装商品中的讲师列表
     * @param goodsResList
     */
    private void assignGoodsSku(List<GoodsRes> goodsResList) {
        if (CollectionUtil.isEmpty(goodsResList)) {
            return;
        }
        // 集合中所有老师id 通过流转化所有对象的idList放在一起后续逐个获取到对象
        List<Long> goodsIdList = goodsResList.stream().map(GoodsRes::getId).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(goodsIdList)) {
            return;
        }
        GoodsSkuDo skuDo = new GoodsSkuDo();
        skuDo.setGoodsIdList(goodsIdList);
        List<GoodsSkuPo> goodsSkuList = goodsSkuService.listPoByDo(skuDo);
        goodsResList.forEach(goodsRes -> {
            List<GoodsSkuPo> skuList = goodsSkuList.stream()
                    .filter(item -> item.getGoodsId().equals(goodsRes.getId()))
                    .collect(Collectors.toList());
            goodsRes.setSkuList(skuList);
        });
    }

    /**
     *
     * @param teacherList
     * @param teacherIdList
     * @return
     */
    private List<TeacherRes> convertTeacherList(List<Teacher> teacherList, List<Long> teacherIdList) {
        return teacherList.stream()
                .filter(teacher -> teacherIdList.contains(teacher.getId()))
                .map(teacher -> BeanUtil.toBean(teacher, TeacherRes.class))
                .collect(Collectors.toList());
    }

    private void saveBatchSku(GoodsReq goodsReq) {
        List<GoodsSkuPo> skuList = goodsReq.getSkuList();

        Long goodsId = goodsReq.getId();

        List<GoodsSku> goodsSkuList = new ArrayList<>();
        for (GoodsSkuPo skuPo : skuList){
            GoodsSku sku = new GoodsSku();
            BeanUtils.copyBeanProp(sku, skuPo);
            // id也会被赋值过来,导致插入失败
            sku.setId(null);
            sku.setGoodsId(goodsId);
            goodsSkuList.add(sku);
        }

        if (goodsSkuList.size() > 0){
            goodsSkuService.saveBatch(goodsSkuList);
        }
    }

    public void assignOtherInfo(List<GoodsRes> goodsResList) {
        // 课程类型名称
        Map<Long, String> dictMap = globalCache.cacheDictMap(Lists.newArrayList(DictCodeEnum.COURSE_TYPE.getCode()));
        goodsResList.forEach(goodsRes -> goodsRes.setCategoryName(dictMap.get(goodsRes.getCategoryId())));
    }
}